package com.selen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.selen.dto.PageDTO;
import com.selen.dto.SchoolInsertDTO;
import com.selen.dto.SchoolUpdateDTO;
import com.selen.entities.Room;
import com.selen.entities.School;
import com.selen.exception.ServiceException;
import com.selen.mapper.SchoolMapper;
import com.selen.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Selen
 */
@Service
@CacheConfig(cacheNames = "school")
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public int insert(SchoolInsertDTO dto) {
        School school = BeanUtil.copyProperties(dto, School.class);
        school.setId(null);
        school.setDeleted(0);
        school.setCreated(LocalDateTime.now());
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.insert(school);
        if (result <= 0){
            throw new ServiceException("DB 插入失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null ", unless = "#result == null ")
    @Override
    public School selectById(Long id) {
        School result = schoolMapper.selectById(id);
        if (ObjectUtil.isNull(result)){
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public int updateById(SchoolUpdateDTO dto) {
        School school = BeanUtil.copyProperties(dto, School.class);
        school.setCreated(null);
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.updateById(school);
        if (result <= 0){
            throw new ServiceException("DB 修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        // todo
        int result = schoolMapper.logicDeleteById(id);
        if (result <= 0){
            throw new ServiceException("DB 逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        // todo
        int result = schoolMapper.logicDeleteByIds(ids);
        if (result <= 0){
            throw new ServiceException("DB 批量逻辑删除失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<School> selectAll() {
        return schoolMapper.selectAll();
    }

    @Cacheable(key = "#root.methodName + ':' + #dto.pageNum + ':' + #dto.pageSize ",
            unless = "#result == null ")
    @Override
    public PageInfo<School> page(PageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(schoolMapper.selectAll());
    }


}
